#include <QComboBox>
#include <QPushButton>

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "errnum.h"


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , u_ui(new Ui::MainWindow)
    , u_mcuList(new MCUList())
{
    /*初始化区域*/
    u_ui->setupUi(this);
    QStringList* namelist = u_mcuList->nameList();
    u_currentName = namelist->first();
    u_currentObj = u_mcuList->currentMCUObj(u_currentName);
    u_ui->u_mcuNameComboBox->addItems(*namelist);
    this->u_ui->u_peripheralTreeView->setModel(u_currentObj->peripheralModel());

    /*信号槽区域*/
    //连接combobox和label显示,做切换芯片型号时的动作
    connect(this->u_ui->u_mcuNameComboBox,&QComboBox::currentTextChanged,[=](){
        setCurrentName(this->u_ui->u_mcuNameComboBox->currentText());
    });


    //测试用,可写测试文件，可删除
    connect(this->u_ui->testButton_PB7,&QPushButton::clicked,[=](){
        qDebug() << "pb7 按下";

        QMap<QString, QStringList*>* deviceMap = this->u_mcuList->currentMCUObj(u_currentName)->u_peripheralTypes;  // 获取你的 QMap 指针

        qDebug()<<this->u_mcuList->currentMCUObj(u_currentName)->u_peripheralModel->GetJsonData(deviceMap);

    });

}

MainWindow::~MainWindow()
{
    delete u_ui;
    delete u_mcuList;
}

QString MainWindow::currentName()
{
    return u_currentName;
}

int MainWindow::setCurrentName(QString name)
{
    u_ui->u_mcuTextLabel->setText(name);
    u_currentName = name;
    return NONE_ERR;
}

